package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmplyeeService;

import com.sun.org.apache.regexp.internal.RE;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@Slf4j
@RestController
//配置以及路径
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmplyeeService emplyeeService;
    //参数有三种：Json格式的参数 {key:value,key:value}
    @RequestMapping("/login")
    public R<Employee> login(@RequestBody Employee empolyee, HttpSession session){
        String password = empolyee.getPassword();
        String md5password = DigestUtils.md5DigestAsHex(password.getBytes());
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,empolyee.getUsername());
        Employee one = emplyeeService.getOne(queryWrapper);
        if(one==null){
            return R.error("用户不存在");
        }
        if(!one.getPassword().equals(md5password)){
            return R.error("密码错误");
        }
        if(one.getStatus() == 0){
            return R.error("账号已禁用");
        }
        session.setAttribute("employee",one.getId());
        return R.success(one);

    }

    @PostMapping("/logout")
    public R<String> logout(HttpSession session){
        //清除session中保存的内容
//        session.removeAttribute("employee");
        //退出得特别干净：密码，用户名，手机号
        session.invalidate();
        return R.success("退出成功");
    }

    /**
     * 保存员工：一级路径：employee，没有二级路径
     * 提交方式：POST
     * 参数类型：JSON格式
     * @param employee
     * @param session
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Employee employee){
        log.info("新增员工纪念馆，员工信息：{}",employee.toString());
        //设置初始密码：123456，需要进行md5加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //设置时间
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
        //设置添加员工的创建者信息
//        Long empId = (Long) session.getAttribute("employee");
//        employee.setCreateUser(empId);
//        employee.setUpdateUser(empId);
        //添加员工操作
        emplyeeService.save(employee); //id才会通过雪花算法算出来
        return R.success("新增员工成功");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<Employee>>   page(Integer page ,Integer pageSize ,String name){
        //1. 创建分页对象
        Page<Employee>  pageInfo = new Page<>(page, pageSize);
        //2. 创建查询对象,添加条件
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //最后修改的排在前面
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        //3.执行page方法
        //生成两条语句：一条，根据条件查询结果集，records里面
        //另外一条：查询记录数 ，放到total
        //把结果封装到pageInfo里
        emplyeeService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);

    }

    /**
     * 启用禁用功能，就是修改
     * @param employee
     * @param session
     * @return
     */
    @PutMapping

    public R<String> update(@RequestBody Employee employee,HttpSession session){

        log.info("修改员工。。。。");
        if (employee.getId()==1){
            return R.error("管理员账号不能修改");
        }

//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser((Long) session.getAttribute("employee"));
        //update .... set xxx=status,update_time=...,update_user =... where id =.....;
        emplyeeService.updateById(employee);
        return R.success("修改成功");
    }


    /**
     * 根据id查询员工信息
     * @param empId
     * @return
     */
    @GetMapping("{empId}")
    public R<Employee> getById(@PathVariable Long empId){
        log.info("获取员工信息");
        Employee employee = emplyeeService.getById(empId);
        return R.success(employee);
    }

}
